package com.macro.mall.portal.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beust.jcommander.internal.Lists;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsTourismProjectActivitiesDetail;
import com.macro.mall.dto.BmsTourismProjectActivitiesParam;
import com.macro.mall.mapper.BmsTourismProjectActivitiesApplicantMapper;
import com.macro.mall.mapper.BmsTourismProjectActivitiesMapper;
import com.macro.mall.model.BmsTourismProjectActivities;
import com.macro.mall.model.BmsTourismProjectActivitiesApplicant;
import com.macro.mall.model.BmsTourismProjectActivitiesApplicantExample;
import com.macro.mall.model.BmsTourismProjectActivitiesExample;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.BmsTourismProjectActivitiesPicService;
import com.macro.mall.portal.service.BmsTourismProjectActivitiesService;
import com.macro.mall.portal.util.MemberUtils;

@Service
public class BmsTourismProjectActivitiesServiceImpl implements BmsTourismProjectActivitiesService {

    @Autowired
    private BmsTourismProjectActivitiesMapper bmsTourismProjectActivitiesMapper;

    @Autowired
    private BmsTourismProjectActivitiesPicService bmsTourismProjectActivitiesPicService;

    @Autowired
    private BmsTourismProjectActivitiesApplicantMapper bmsTourismProjectActivitiesApplicantMapper;

    @Override
    public List<BmsTourismProjectActivities> getList(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        if (Objects.isNull(bmsTourismProjectActivitiesParam.getTourismProjectId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsTourismProjectActivitiesParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsTourismProjectActivitiesExample example = new BmsTourismProjectActivitiesExample();
        BmsTourismProjectActivitiesExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        criteria.andTourismProjectIdEqualTo(bmsTourismProjectActivitiesParam.getTourismProjectId());
        if (Objects.nonNull(bmsTourismProjectActivitiesParam.getIsLongTerm())) {
            criteria.andIsLongTermEqualTo(bmsTourismProjectActivitiesParam.getIsLongTerm());
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesParam.getIsFree())) {
            criteria.andIsFreeEqualTo(bmsTourismProjectActivitiesParam.getIsFree());
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesParam.getIsLimitPeopleNumber())) {
            criteria.andIsLimitPeopleNumberEqualTo(bmsTourismProjectActivitiesParam.getIsLimitPeopleNumber());
        }
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesParam.getTitle())) {
            criteria.andTitleLike("%" + bmsTourismProjectActivitiesParam.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(bmsTourismProjectActivitiesParam.getAddress())) {
            criteria.andTitleLike("%" + bmsTourismProjectActivitiesParam.getAddress() + "%");
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesParam.getOpeningTime())) {
            criteria.andOpeningTimeGreaterThanOrEqualTo(bmsTourismProjectActivitiesParam.getOpeningTime());
        }
        if (Objects.nonNull(bmsTourismProjectActivitiesParam.getCloseingTime())) {
            criteria.andCloseingTimeGreaterThanOrEqualTo(bmsTourismProjectActivitiesParam.getCloseingTime());
        }
        example.setOrderByClause("create_time desc");
        List<BmsTourismProjectActivities> list = bmsTourismProjectActivitiesMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsTourismProjectActivitiesDetail getItem(Long id) {

        BmsTourismProjectActivities bmsTourismProjectActivities = bmsTourismProjectActivitiesMapper.selectByPrimaryKey(id);
        BmsTourismProjectActivitiesDetail detail = new BmsTourismProjectActivitiesDetail();
        BeanUtils.copyProperties(bmsTourismProjectActivities, detail);
        // 获取封面图
        String coverPic = bmsTourismProjectActivitiesPicService.findCoverPicByActivitiesId(bmsTourismProjectActivities.getId());
        // 获取活动图片
        List<String> picList = bmsTourismProjectActivitiesPicService.findPicListByActivitiesId(bmsTourismProjectActivities.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Override
    public List<BmsTourismProjectActivitiesDetail> getHotProjectActivities(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        Integer pageNum = bmsTourismProjectActivitiesParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsTourismProjectActivitiesExample example = new BmsTourismProjectActivitiesExample();
        BmsTourismProjectActivitiesExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        criteria.andTourismProjectIdEqualTo(bmsTourismProjectActivitiesParam.getTourismProjectId());
        criteria.andCloseingTimeGreaterThanOrEqualTo(new Date());
        example.setOrderByClause("create_time desc");
        List<BmsTourismProjectActivities> list = bmsTourismProjectActivitiesMapper.selectByExample(example);
        BmsTourismProjectActivitiesDetail bmsTourismProjectActivitiesDetail;
        String coverPic;
        List<String> picList;
        List<BmsTourismProjectActivitiesDetail> detailList = Lists.newArrayList();
        for (BmsTourismProjectActivities bmsTourismProjectActivities : list) {
            bmsTourismProjectActivitiesDetail = new BmsTourismProjectActivitiesDetail();
            BeanUtils.copyProperties(bmsTourismProjectActivities, bmsTourismProjectActivitiesDetail);
            // 获取封面图
            coverPic = bmsTourismProjectActivitiesPicService.findCoverPicByActivitiesId(bmsTourismProjectActivities.getId());
            // 获取活动图片
            picList = bmsTourismProjectActivitiesPicService.findPicListByActivitiesId(bmsTourismProjectActivities.getId());

            bmsTourismProjectActivitiesDetail.setCoverPic(coverPic);
            bmsTourismProjectActivitiesDetail.setPicList(picList);
            detailList.add(bmsTourismProjectActivitiesDetail);
        }
        return detailList;
    }

    @Override
    public List<BmsTourismProjectActivitiesDetail> getTestimonialsProjectActivities(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        Integer pageNum = bmsTourismProjectActivitiesParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsTourismProjectActivitiesExample example = new BmsTourismProjectActivitiesExample();
        BmsTourismProjectActivitiesExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        criteria.andTourismProjectIdEqualTo(bmsTourismProjectActivitiesParam.getTourismProjectId());
        criteria.andCloseingTimeGreaterThanOrEqualTo(new Date());
        criteria.andIdNotEqualTo(bmsTourismProjectActivitiesParam.getId());
        example.setOrderByClause("create_time desc");
        List<BmsTourismProjectActivities> list = bmsTourismProjectActivitiesMapper.selectByExample(example);
        BmsTourismProjectActivitiesDetail bmsTourismProjectActivitiesDetail;
        String coverPic;
        List<String> picList;
        List<BmsTourismProjectActivitiesDetail> detailList = Lists.newArrayList();
        for (BmsTourismProjectActivities bmsTourismProjectActivities : list) {
            bmsTourismProjectActivitiesDetail = new BmsTourismProjectActivitiesDetail();
            BeanUtils.copyProperties(bmsTourismProjectActivities, bmsTourismProjectActivitiesDetail);
            // 获取封面图
            coverPic = bmsTourismProjectActivitiesPicService.findCoverPicByActivitiesId(bmsTourismProjectActivities.getId());
            // 获取活动图片
            picList = bmsTourismProjectActivitiesPicService.findPicListByActivitiesId(bmsTourismProjectActivities.getId());

            bmsTourismProjectActivitiesDetail.setCoverPic(coverPic);
            bmsTourismProjectActivitiesDetail.setPicList(picList);
            detailList.add(bmsTourismProjectActivitiesDetail);
        }
        return detailList;
    }

    @Override
    public List<BmsTourismProjectActivitiesDetail> getReserveProjectActivities(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam, HttpServletRequest request) {

        UmsMember wxUser = MemberUtils.getUser();

        if (Objects.isNull(wxUser)) {
            throw new NullPointerException("wxUser is null");
        }
        Integer pageNum = bmsTourismProjectActivitiesParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsTourismProjectActivitiesApplicantExample example = new BmsTourismProjectActivitiesApplicantExample();
        BmsTourismProjectActivitiesApplicantExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        criteria.andCreateUserEqualTo(wxUser.getId());
        List<String> status = bmsTourismProjectActivitiesParam.getStatus();
        if (CollectionUtils.isNotEmpty(status)) {
            criteria.andStatusIn(status);
        }
        List<BmsTourismProjectActivitiesApplicant> list = bmsTourismProjectActivitiesApplicantMapper.selectByExample(example);

        List<Long> activitiesIdList = list.parallelStream().filter(i -> Objects.nonNull(i) && Objects.nonNull(i.getTourismProjectActivitiesId())).map(BmsTourismProjectActivitiesApplicant::getTourismProjectActivitiesId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(activitiesIdList)) {
            return Collections.emptyList();
        }
        BmsTourismProjectActivitiesExample activitiesExample = new BmsTourismProjectActivitiesExample();
        BmsTourismProjectActivitiesExample.Criteria activitiesExampleCriteria = activitiesExample.createCriteria();
        activitiesExampleCriteria.andIsDelete(0);
        activitiesExampleCriteria.andIdIn(activitiesIdList);
        List<BmsTourismProjectActivities> activitiesList = bmsTourismProjectActivitiesMapper.selectByExample(activitiesExample);
        List<BmsTourismProjectActivitiesDetail> detailList = Lists.newArrayList();
        BmsTourismProjectActivitiesDetail bmsTourismProjectActivitiesDetail;
        String coverPic;
        List<String> picList;
        for (BmsTourismProjectActivities bmsTourismProjectActivities : activitiesList) {
            bmsTourismProjectActivitiesDetail = new BmsTourismProjectActivitiesDetail();
            BeanUtils.copyProperties(bmsTourismProjectActivities, bmsTourismProjectActivitiesDetail);
            // 获取封面图
            coverPic = bmsTourismProjectActivitiesPicService.findCoverPicByActivitiesId(bmsTourismProjectActivities.getId());
            // 获取活动图片
            picList = bmsTourismProjectActivitiesPicService.findPicListByActivitiesId(bmsTourismProjectActivities.getId());

            bmsTourismProjectActivitiesDetail.setCoverPic(coverPic);
            bmsTourismProjectActivitiesDetail.setPicList(picList);
            detailList.add(bmsTourismProjectActivitiesDetail);
        }
        return detailList;
    }

    @Override
    public int getActivitiesHasRegistrationNum(Long activitiesId) {
        UmsMember user = MemberUtils.getUser();
        return bmsTourismProjectActivitiesMapper.getActivitiesHasRegistrationNum(activitiesId, user.getId());
    }

    @Override
    public boolean isHasApplicant(Long id) {
        int num = this.getActivitiesHasRegistrationNum(id);
        return num > 0;
    }

}
